/**
 * ...
 * @author Yog
 */

package cookinmaddy.client;
import com.yff.controls.FButton;
import com.yff.controls.FDiv;
import com.yff.controls.FInput;
import com.yff.controls.FList;
import com.yff.controls.FListNode;
import com.yff.controls.FText;
import cookinmaddy.client.view.RecipeControlView;
import cookinmaddy.model.Recipe;
import haxe.Serializer;
import haxe.Unserializer;
import js.Dom;


class RecipeView 
{
	public static var DOC_ID : String = "";

	var mainDoc : FDiv;
	var innerDoc : FDiv;	
	var addDiv : FDiv;
	
	var list : List<Recipe> ;
	var self : RecipeView;
	
	public function new() {	self = this; }
	
	public function show()
	{
		if (mainDoc == null)
		{
			mainDoc = FDiv.getById(DOC_ID);
			var req = "?action=listrecipes";
			StringTools.htmlEscape(req);
			Ajax.getContentAsync(req, onAjaxReturn_LoadList);
		}
		else
		{
			load();
		}
	}	
	
	private function onAjaxReturn_LoadList(data : Dynamic)
	{
		list = Unserializer.run(data);
		load();
	}
	
	private function load()
	{
		var ulist : FList = new FList();
		for (recipe in list)
		{	
			var node :FListNode = new FListNode();
			
			var editbtn : FButton = new FButton("Edit");
			editbtn.addEvent("click", self.onClick_Edit, { node: node, recipe : recipe});

			var delbtn : FButton = new FButton("Delete");
			delbtn.addEvent("click", self.onClick_Delete, { node: node, recipe : recipe } );
			
			node.innerHTML = recipe.Name;
			node.appendChild(delbtn);
			node.appendChild(editbtn);
			ulist.appendChild(node);
			
		}
		innerDoc = new FDiv();
		innerDoc.id = "innerDoc";
		innerDoc.appendChild(ulist);
		if (addDiv == null)
		{
			var btn : FButton  = new FButton("Add");
			btn.addEvent("click", self.onClick_DoAdd, null);
			addDiv = new FDiv();
			addDiv.appendChild(btn);
		}
		innerDoc.appendChild(addDiv);		
		mainDoc.appendChild(innerDoc);	
	}
	
	private function onClick_Edit(input : Dynamic)
	{
		var node : FListNode = input.node;
		var recipe : Recipe = input.recipe;
		node.innerHTML = "";
		var innerDiv : FDiv  = new FDiv();
		node.clear();
		node.appendChild(innerDiv);
		var recipe : Recipe = new Recipe();
		var recipeadd : RecipeControlView = new RecipeControlView(self.onAjaxReturn_Updated, innerDiv, recipe, false);
		recipeadd.show();
		
		/*
		node.appendChild(new FText("Name: "));
		var name_input : FInput = new FInput();
		name_input.defaultValue = recipe.Name;
		name_input.id = "name";		
		node.appendChild(name_input);
		
		var btn : FButton = new FButton("Update");
		btn.addEvent("click", self.onClick_Update, { node : node, recipe: recipe } );
		node.appendChild(btn);
		
		btn = new FButton("Cancel");
		btn.addEvent("click", self.show, null);
		node.appendChild(btn);*/
	}

	private function onClick_Delete(args : Dynamic)
	{
		var node : FListNode = args.node;
		var recipe : Recipe = args.recipe;
		var data = Serializer.run(recipe);
		var req = "?action=delRecipe&data=" + data;
		StringTools.htmlEscape(req);
		Ajax.getContentAsync(req, onAjaxReturn_Deleted);		
	}	
	
	private function onAjaxReturn_Deleted(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var id : Int = cast data;
			for (unit in list)
			{
				if (unit.ID == id)
				{
					list.remove(unit);
					show();
					break;
				}
			}			
		}
	}
	
	private function onClick_DoAdd()
	{
		addDiv.innerHTML = "";
		var recipe : Recipe = new Recipe();
		var recipeadd : RecipeControlView = new RecipeControlView(self.onAjaxReturn_Added, addDiv, recipe, true);
		recipeadd.show();
	}
	
	private function onAjaxReturn_Added(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret != "KO")
		{
			var Recipe : Recipe = haxe.Unserializer.run(data);
			list.add(Recipe);
			show();
		}
		else
			js.Lib.alert(ret);
	}
	
	private function onClick_Update(node : HtmlDom, Recipe : Recipe)
	{
		var l : Int = node.childNodes.length;
		for(i in 0...l)
		{
			var txt : Dom.Text = cast node.childNodes[i] ;
			switch(txt.id)
			{
				case "name":
					Recipe.Name = txt.value;
				default:
			}
		}
		
		var data = Serializer.run(Recipe);
		var req = "?action=updateRecipe&data=" + StringTools.urlEncode(data);
		
		Ajax.getContentAsync(req, onAjaxReturn_Updated);
	}	
	
	private function onAjaxReturn_Updated(data : Dynamic)
	{
		var ret : String = cast data;
		if (ret == "OK")
			show();
		else
			js.Lib.alert(ret);
	}	
}